<?php

class MW_Simplenews_NewsController extends Mage_Core_Controller_Front_Action
{
	
	public function indexAction($id)
	{
		$this->loadLayout();
		$id = $this->getRequest()->getParams('id');
		$category = Mage::getModel('simplenews/news')->load($id)->getData();
		$this->getLayout()->getBlock('head')->setTitle($category['meta_title']);
		$this->getLayout()->getBlock('head')->setKeywords($category['meta_keywords']);
		$this->getLayout()->getBlock('head')->setDescription($category['meta_description']);

		$this->renderLayout();
	}

	public function userAction(){
		$this->loadLayout();
		$this->renderLayout();

		if($data = $this->getRequest()->getPost()){

			if(isset($_FILES['news_images']['name']) && $_FILES['news_images']['name'] != '') {
				try {	
					$uploader = new Varien_File_Uploader('news_images');
	           		$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
					$uploader->setAllowRenameFiles(false);
					$uploader->setFilesDispersion(false);
					$path = Mage::getBaseDir('media') . DS .'simplenews'. DS ;
					$uploader->save($path, $_FILES['news_images']['name'] );
					
				}catch (Exception $e) {
		        }
	  			$data['images'] = $_FILES['news_images']['name'];
			}

			$model = Mage::getModel('simplenews/news');		
			$model->setData('title', $data['title']);
			$model->setData('url_key', $data['url_key']);
			// $model->setData('status', $data['status']);
			$model->setData('status', 2);
			$model->setData('category_id', $data['category_id']);
			$model->setData('images', $data['images']);
			$model->setData('summary', $data['summary']);
			$model->setData('content', $data['content']);
			

			try {
				// Zend_debug::dump($model->getData()); die();
				$model->save();
				Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('simplenews')->__('Item was successfully saved'));
				Mage::getSingleton('adminhtml/session')->setFormData(false);
                $this->_redirect('simplenews/news/index');

				return;
            } catch (Exception $e) {
            	Mage::log($e->getMessage()); 
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setFormData($data);
                $this->_redirect('simplenews/news/user	');
                return;
            }
		
		}
	}
}